<template>
    <div>
        <h2>会员添加</h2>
        <table class="table table-bordered">
            <tbody>

                <tr>
                    <td>编号</td>
                    <td>
                        <input type="text" v-model="quertinfo.MemberCode" disabled/>
                    </td>
                </tr>

                <tr>
                    <td>电话</td>
                    <td>
                        <input type="text" v-model="quertinfo.Phone" maxlength="11"/>
                    </td>
                </tr>

                <tr>
                    <td>会员名</td>
                    <td>
                        <input type="text" v-model="quertinfo.UserName"/>
                    </td>
                </tr>

                <tr>
                    <td>身份证号码</td>
                    <td>
                        <input type="text" v-model="quertinfo.Card" max="18"/>
                    </td>
                </tr>

                <tr>
                    <td>等级</td>
                    <td>
                        <select v-model="quertinfo.LevelId">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in cc.data" :key="item.Id">{{item.LevelName}}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td>生日</td>
                    <td>
                        <input type="date" v-model="quertinfo.Birth"/>
                    </td>
                </tr>

                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" name="Sex" :value="1" v-model="quertinfo.Sex"/>男
                        <input type="radio" name="Sex" :value="0" v-model="quertinfo.Sex"/>女
                    </td>
                </tr>

                <tr>
                    <td>积分</td>
                    <td>
                        <input type="text" v-model="quertinfo.JiFen"/>
                    </td>
                </tr>

                <tr>
                    <td>余额</td>
                    <td>
                        <input type="text" v-model="quertinfo.Money"/>
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-primary" @click="add"/>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue'
import axios from 'axios';
import {useRouter} from 'vue-router'
const router=useRouter();
import moment from 'moment';

//周期函数
onMounted(()=>{
    getlevel();
})

//下拉绑定
const cc:any=reactive({
    data:[]
})
const getlevel=()=>{
    //发送网络请求
    axios({
        method:'get',
        url:'/api/Member/GetLevel'
    })
    .then(res=>{
        console.log(res);
        cc.data=res.data;
    })
    .catch(err=>{
        console.error(err);
    })
}

var num =parseInt((Math.random()*1000).toString())+100;
//添加
let quertinfo=reactive(
    {
  "MemberCode": moment(new Date).format('YYYYMMDD')+num,
  "Phone": "",
  "UserName": "",
  "Card": "",
  "LevelId": '',
  "Birth": "",
  "Sex": 0,
  "JiFen": "",
  "Money": ""
}
)

const add=()=>{
    //有效性判断
    if(quertinfo.Phone==""){
        alert('手机号不能为空');
        return;
    }

    let reg=/^1[3-9]\d{9}$/;
    if(!reg.test(quertinfo.Phone)){
        alert('手机号格式不对');
        return;
    }

    if(quertinfo.UserName==""){
        alert('会员名不能为空');
        return;
    }
    if(quertinfo.Card==""){
        alert('身份证号不能为空');
        return;
    }
    let reg1=/^\d{17}[0-9xX]$/;
    if(!reg1.test(quertinfo.Card)){
        alert('身份证格式不对');
        return;
    }

    if(quertinfo.Birth==""){
        alert('生日不能为空');
        return;
    }

    if(quertinfo.JiFen==""){
        alert('积分不能为空');
        return;
    }

    // if(isNaN(quertinfo.JiFen)==true)
    // {
    //     alert('积分必须是数值');
    //     return;
    // }

    if(quertinfo.Money==""){
        alert('余额不能为空');
        return;
    }
    // if(isNaN(quertinfo.Money)==true)
    // {
    //     alert('余额必须是数值');
    //     return;
    // }

    //返回网络请求
    axios({
        method:'post',
        url:'/api/Member/AddMember',
        data:quertinfo
    })
    .then(res=>{
        console.log(res);
        if(res.data==-1){
            alert('手机号已存在');
            return;
        }
        if(res.data==-2){
            alert('身份证号已存在');
            return;
        }
        if(res.data>0){
            alert('成功');
            router.push('/memberlist')
        }
        else{
            alert('失败');
        }
    })
}

</script>

<style scoped>

</style>